package itman.elm.dao.impl;

import itman.elm.dao.DeliveryAddressDao;
import itman.elm.po.DeliveryAddress;
import itman.elm.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressDaoImpl implements DeliveryAddressDao {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;

    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws Exception {
        List<DeliveryAddress> list = new ArrayList<>();
        String sql = "select * from deliveryderss where userId=? order by daId";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,userId);

            rs = pstm.executeQuery();
            while (rs.next()){
                DeliveryAddress deliveryAddress = new DeliveryAddress();
                deliveryAddress.setDaId(rs.getInt("daId"));
                deliveryAddress.setContactName(rs.getString("contactName"));
                deliveryAddress.setContactSex(rs.getInt("contactSex"));
                deliveryAddress.setContactTel(rs.getString("contactTel"));
                deliveryAddress.setAddress(rs.getString("address"));
                deliveryAddress.setUserId(rs.getString("userId"));
                list.add(deliveryAddress);
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstm);
        }
        return list;
    }

    @Override
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception {
        int result = 0;
        String sql = "insert into deliveryaddress values(null,?,?,?,?,?)";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,deliveryAddress.getContactName());
            pstm.setInt(2,deliveryAddress.getContactSex());
            pstm.setString(3, deliveryAddress.getContactTel());
            pstm.setString(4, deliveryAddress.getAddress());
            pstm.setString(5, deliveryAddress.getUserId());

            result = pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pstm);
        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) throws Exception {
        int result = 0;
        String sql = "delete from deliveryaddress where daId=?";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, daId);
            result = pstm.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstm);
        }
        return result;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(Integer daId) throws Exception {
        DeliveryAddress deliveryAddress = null;
        String sql = "select * from deliveryaddress where daId=?";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, daId);
            rs = pstm.executeQuery();
            while (rs.next()){
                DeliveryAddress deliveryAddress1 = new DeliveryAddress();
                deliveryAddress1.setDaId(rs.getInt("daId"));
                deliveryAddress.setContactName(rs.getString("contactName"));
                deliveryAddress.setContactSex(rs.getInt("contactSet"));
                deliveryAddress.setContactTel(rs.getString("contactTel"));
                deliveryAddress.setAddress(rs.getString("address"));
                deliveryAddress.setUserId(rs.getString("userId"));

            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstm);
        }
        return deliveryAddress;
    }

    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception {
        int result =0;
        String sql = "update deliveryaddress set contactName=?,contactSet=?,contactTel=?,address=?,userId=? where daId=?";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, deliveryAddress.getContactName());
            pstm.setInt(2, deliveryAddress.getContactSex());
            pstm.setString(3, deliveryAddress.getContactTel());
            pstm.setString(4, deliveryAddress.getAddress());
            pstm.setString(5, deliveryAddress.getUserId());
            pstm.setInt(6, deliveryAddress.getDaId());
            result = pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
}
